package net.zdreamer.geracomium.ui;

import java.util.ArrayList;
import java.util.List;

import net.zdreamer.geracomium.adapter.FragmentTabAdapter;
import net.zdreamer.geracomium.utils.window.ViewControlor;

import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.geracomium.R;

public class MainActivity extends FragmentActivity {
    private RadioGroup radio_group;
    private TextView title;
    private ImageView back;
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private long firstTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fragments.add(new HomeFragment());
        fragments.add(new MoreFragment());

        radio_group = (RadioGroup) findViewById(R.id.tabs_rg);
        ViewControlor.setSize(this, radio_group, 640, 98, true);
        title = (TextView) findViewById(R.id.title_content);
        back = (ImageView) findViewById(R.id.back);
        back.setVisibility(View.GONE);
        title.setText("主页");
        FragmentTabAdapter tabAdapter = new FragmentTabAdapter(this, fragments,
                R.id.tab_content, radio_group, false);
        tabAdapter
                .setOnRgsExtraCheckedChangedListener(new FragmentTabAdapter.OnRgsExtraCheckedChangedListener() {
                    @Override
                    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup,
                                                         int checkedId, int index) {
                        if (checkedId == R.id.activity_home) {
                            title.setText("主页");
                        } else if (checkedId == R.id.more_activity) {
                            title.setText("更多");
                        }
                    }
                });
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    Toast.makeText(MainActivity.this, "再按一次退出程序",
                            Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;
                    return true;
                } else {
                    // System.exit(0);
                    int currentVersion = android.os.Build.VERSION.SDK_INT;
                    if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
                        Intent startMain = new Intent(Intent.ACTION_MAIN);
                        startMain.addCategory(Intent.CATEGORY_HOME);
                        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(startMain);
                        System.exit(0);
                    } else {// android2.1
                        ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                        am.restartPackage(getPackageName());
                    }

                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }
}
